package com.shop2cn.iapi.prodstock.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.prodstock.parameter.ReturnProductActivityStockBean;
import com.shop2cn.iapi.prodstock.service.ReturnProductActivityStockCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DetailsCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallServiceV2;
import com.ymttest.database.sqlwapper.GodProductActivityStockWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.YMTDateUtil;
/**
*活动结束返库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ReturnProductActivityStock{
	private static ReturnProductActivityStockBean returnproductactivitystockBean;
	private static ReturnProductActivityStockCall returnproductactivitystockCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("活动结束返库存Test");
	}

	@Before
	public void caseUp() {
		returnproductactivitystockBean=new ReturnProductActivityStockBean();
		returnproductactivitystockCall=new ReturnProductActivityStockCall();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnproductactivitystockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	//渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductActivityStock_001() {
		Logger.start(true,"活动结束返库存，返还10真实库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,10, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			int ProductInActivityId = new GodProductActivityStockWapper().selectProductInActivityId(productId, activityId);
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(rootUserId,productId);
			
			returnproductactivitystockBean.setActivityID(activityId);
			returnproductactivitystockBean.setProductID(productId);
			returnproductactivitystockBean.setProductActivityId(ProductInActivityId);
			returnproductactivitystockBean.setActivityType(initProductaddactivity.getActivityType());
			returnproductactivitystockBean.setUserID(rootUserId);
			returnproductactivitystockBean.setBeginTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getBeginTime()));
			returnproductactivitystockBean.setEndTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getEndTime()));
			
			returnproductactivitystockCall.setData(returnproductactivitystockBean);
			returnproductactivitystockCall.callService();			
//			Logger.verifyEquals("200", returnproductactivitystockCall.getString("Code"), "验证code");
			
			ProductDetailsDto selectProductDetails1 = AddAgentProductCallService.selectProductDetails(rootUserId,productId);
			equalsDetails(selectProductDetails, selectProductDetails1, 10);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductActivityStock_002() {
		Logger.start(true,"活动结束返库存，使用规格库存，不会返库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			int ProductInActivityId = new GodProductActivityStockWapper().selectProductInActivityId(productId, activityId);
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(rootUserId,productId);
			
			returnproductactivitystockBean.setActivityID(activityId);
			returnproductactivitystockBean.setProductID(productId);
			returnproductactivitystockBean.setProductActivityId(ProductInActivityId);
			returnproductactivitystockBean.setActivityType(initProductaddactivity.getActivityType());
			returnproductactivitystockBean.setUserID(rootUserId);
			returnproductactivitystockBean.setBeginTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getBeginTime()));
			returnproductactivitystockBean.setEndTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getEndTime()));
			
			returnproductactivitystockCall.setData(returnproductactivitystockBean);
			returnproductactivitystockCall.callService();			
//			Logger.verifyEquals("200", returnproductactivitystockCall.getString("Code"), "验证code");
			
			ProductDetailsDto selectProductDetails1 = AddAgentProductCallService.selectProductDetails(rootUserId,productId);
			equalsDetails(selectProductDetails, selectProductDetails1, 0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	public void equalsDetails(ProductDetailsDto d1,ProductDetailsDto d2,int num) {
		List<DetailsCatalogDto> cata1 = d1.getCatalogList();
		List<DetailsCatalogDto> cata2 = d2.getCatalogList();
		for (int i = 0; i < cata1.size(); i++) {
			DetailsCatalogDto Dto1 = cata1.get(i);
			DetailsCatalogDto Dto2 = cata2.get(i);
			Logger.verifyEquals(Dto1.getRealStock()+num, Dto2.getRealStock(), "验证真实库存");
			Logger.verifyEquals(Dto1.getStock()+num, Dto2.getStock(), "验证库存");
//			Logger.verifyEquals(Dto1.getVirtualStock()+num, Dto2.getVirtualStock(), "验证虚拟库存");
		}
		
	}
}